package com.key.w8.http1.factory;

import com.key.w8.http1.connection.Connection;
import com.key.w8.http1.entity.HttpRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author: k8
 * @CreateTime: 2025-04-27
 * @Version: 1.0
 */
public class RequestBuilder {

    private HttpMethod httpMethod = HttpMethod.GET;
    private HttpHeaders httpHeaders = new DefaultHttpHeaders(true);
    private Map<String,List<String>> paras;
    private HttpVersion httpVersion = HttpVersion.HTTP_1_1;
    private String uri;
    public RequestBuilder() {
    }
    public HttpRequest build(){
        HttpRequest httpRequest = new HttpRequest(httpHeaders, httpVersion, httpMethod, uri);
        httpRequest.setParameters(paras);
        return httpRequest;
    }
    public RequestBuilder method(com.key.w8.http1.factory.HttpMethod httpMethod){
        this.httpMethod = com.key.w8.http1.factory.HttpMethod.getMethod(httpMethod);
        return this;
    }
    public RequestBuilder version(com.key.w8.http1.factory.HttpVersion httpVersion){
        this.httpVersion = com.key.w8.http1.factory.HttpVersion.getVersion(httpVersion);
        return this;
    }
    public RequestBuilder uri(String uri){
        this.uri = uri;
        return this;
    }
    public RequestBuilder addHeader(String headerName, String value){
        this.httpHeaders.add(headerName,value);
        return this;
    }
    public void addParameter(String key , String value){
        if (this.paras == null){
            this.paras = new HashMap<>();
        }
        List<String> values = this.paras.get(key);
        if (values == null){
            values = new LinkedList<>();
            this.paras.put(key,values);
        }
        values.add(value);
    }
    public FormDataBuilder formData(){
        return new FormDataBuilder(httpMethod,httpHeaders,httpVersion,uri,paras);
    }




}
